package adv.i31.robot;

import java.io.IOException;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyGLSurfaceView extends GLSurfaceView {

	MyRenderer mRenderer;

	public MyGLSurfaceView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		mRenderer = new MyRenderer();
		this.setEGLContextClientVersion(2);
		this.setRenderer(mRenderer);

		try {
			mRenderer.loadShaders(context);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("Surface View created");
	}

	public MyGLSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		System.out.println(event);

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mRenderer.mRobot.mJoints.get((int) (Math.random() * mRenderer.mRobot.mJoints.size())).mAngle = (float) (Math
					.random() * 180f - 90f);
			Joint j = mRenderer.mRobot.mJoints.get(mRenderer.mRobot.mJoints.size() - 1);
			j.calculateTransformation();
			// System.out.println("down");

			break;

		case MotionEvent.ACTION_MOVE:
			// System.out.println("move");
			// mRenderer.setClearColor(event.getX()/500.0f,event.getY()/500.0f ,
			// 0.0f);
			break;
		}

		// return super.onTouchEvent(event)
		return true;
	}

}
